<?php
// Đệ quy menu chính
function renderMenu($container, $role, $acl ,$i=1){
    foreach($container as $page){
        $class = (empty($page->class)?"":"class='$page->class'");
        
        // Menu của lập trình viên
        if($page->class=="developer" && APPLICATION_ENV!="development") continue;
        
        // Kiểm tra nếu ko có quyền vào trang thì ko render ra menu
        $resource = $page->getResource();
        if($acl->hasRole($role) && $resource!=null)
        {
            if(!$acl->isAllowed($role, $resource, $page->getPrivilege())) continue;
        }
        
        echo "<li $class>";
            if($i==1){
                echo "<a href='$page->uri'><span>$page->label</span></a>";
            }else{
                echo "<a id='$page->id' href='$page->uri'>$page->label</a>";
            }
            if(!empty($page->pages)){
                echo "<ul>";
                    renderMenu($page->pages, $role, $acl ,$i+1);
                echo "</ul>";
            }
        echo "</li>";
    }
}

//Đệ quy menu danh mục bài viết
function addChildPage(&$page, $container, $idcat, $md, $baselink){
    $rows = $md->fetchAll("parent_id=$idcat AND category_status=1");
    if($rows->count()<=0){
        return;
    }
    foreach($rows as $row){
        $childs = $row->findDependentRowset("Application_Model_DbTable_ArticleCategory");
        $n_child = $childs->count();
        $arraw = ($n_child>0)?"<label>»»</label>":"";
        $action = ($n_child>0 || $row->has_child)?"article-category/sub-list":"article/index";
        $link = $baselink."/admin/$action/idcat/$row->idcategories";
        $page->addPage(array(
            'label'=> $row->admin_name."$arraw",
            'uri' => $link,
            'id'=>$row->category_alias."-".$row->idcategories
        ));
        $c_page = $container->findById($row->category_alias."-".$row->idcategories);
        addChildPage($c_page, $container, $row->idcategories, $md, $baselink);
    }
    
}

// Bắt đầu tạo menu danh mục bài viết
$mdCat = new Application_Model_DbTable_ArticleCategory();
$page_article = $this->container->findById("article");
$select = $mdCat->select();
$select->where("parent_id IS NULL")
       ->where("category_status=1")
       ->order("category_local");
$rows = $mdCat->fetchAll($select);

foreach($rows as $row)
{
    $childs = $row->findDependentRowset("Application_Model_DbTable_ArticleCategory");
    $n_child = $childs->count();
    $arraw = ($n_child>0)?"<label>»»</label>":"";
    $action = ($n_child>0 || $row->has_child)?"article-category/sub-list":"article/index";
    $link = $this->baseUrl("/admin/$action/idcat/$row->idcategories");
    $page_article->addPage(array(
        'label'=>$row->admin_name.$arraw,
        'uri'=>$link,
        'id'=>$row->category_alias."-".$row->idcategories
    ));
    $page = $this->container->findById($row->category_alias."-".$row->idcategories);
    addChildPage($page, $this->container, $row->idcategories, $mdCat, $this->baseUrl());  
}

if(APPLICATION_ENV=="development"){
    $page_article->setUri($this->baseUrl("admin/article-category/list"));
    $page_article->setLabel("Danh mục bài viết");
}


$acl = Zend_Registry::get("acl");
$role = Zend_Auth::getInstance()->getIdentity();
$mdGroup = new Application_Model_DbTable_UserGroup();
$role = $mdGroup->find($role->groups_id)->current()->groups_name;
?>

<ul class="nav main">
<?php renderMenu($this->container, $role, $acl)?>
</ul>